<?php
if (!defined('__COLA_DIR__')) exit;

/**
 * 执行模块
 *
 * @author edikud
 * @package Widget
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */
class Widget_Do extends Cola
{
    /**
     * 路由映射
     *
     * @access private
     * @var array
     */
    private $_map = array(	
        'ajax'                      =>  'Widget_Ajax',
        'login'                     =>  'Widget_Users_Login',
        'logout'                    =>  'Widget_Users_Logout',
        'register'                  =>  'Widget_Users_Register',
        'upload'                    =>  'Widget_Upload',
        'comments-edit'             =>  'Widget_Comments_Edit',
        'contents-page-edit'        =>  'Widget_Contents_Page_Edit',
        'contents-post-edit'        =>  'Widget_Contents_Post_Edit',
        'metas-category-edit'       =>  'Widget_Metas_Category_Edit',
        'metas-tag-edit'            =>  'Widget_Metas_Tag_Edit',
        'options-discussion'        =>  'Widget_Options_Discussion',
        'options-general'           =>  'Widget_Options_General',
        'options-permalink'         =>  'Widget_Options_Permalink',
        'options-reading'           =>  'Widget_Options_Reading',
        'plugins-edit'              =>  'Widget_Plugins_Edit',
        'themes-edit'               =>  'Widget_Themes_Edit',
        'users-edit'                =>  'Widget_Users_Edit',
        'users-profile'             =>  'Widget_Users_Profile',
    );

    /**
     * 入口函数,初始化路由器
     *
     * @access public
     * @return void
     * @throws Widget_Exception
     */
    public function execute()
    {
        /** 验证路由地址 **/
        $action = isset($this->request->action) ? $this->request->action : null;

        /** 判断是否为plugin */
        $actionTable = array_merge($this->_map, unserialize($this->widget('Widget_Options')->actionTable));

        if (isset($actionTable[$action])) {
            $this->widget($actionTable[$action])->action();
            return;
        }

        throw new Widget_Exception(_t('请求的地址不存在'), 404);
    }
}
